package com.edu.flink;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * <p>
 *
 * </p>
 *
 * @author jpge
 * @since 2024-04-23
 */
public class BatchJob {

    public static void main(String[] args) throws Exception {
        // 1. 设置运行环境
        final ExecutionEnvironment executionEnvironment = ExecutionEnvironment.getExecutionEnvironment();
//        final ExecutionEnvironment executionEnvironment = ExecutionEnvironment.createRemoteEnvironment("192.168.18.128", 9000);
        // 2. 设置数据源
        DataSource<String> dataSource =
                executionEnvironment

                        .fromElements(
                                "abc abc def lksd disd",
                                "adf dedf def abc"
                        );

        // 3. 处理数据
        dataSource.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
                String[] split = s.split(" ");
                for (String word : split) {
                    collector.collect(new Tuple2<>(word, 1));
                }
            }
        }).groupBy(0).sum(1).print();

        // 执行程序
//        executionEnvironment.execute();
    }

}
